﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NHibernateToSQLMapping.BLL;
using System.Resources;

namespace NHibernateToSQLMapping
{
    public partial class CreateMappingFile : Form
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public CreateMappingFile()
        {
            InitializeComponent();
            skinEngine1.SkinFile = "MP10.SSK";
        }

        /// <summary>
        /// 加载数据库
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cb_DataBase_Click(object sender, EventArgs e)
        {
            if (cb_DataBase.Items.Count > 0)
            {
                return;
            }
            string serverIP = txt_ServerIP.Text.Trim();
            if (string.IsNullOrEmpty(serverIP))
            {
                MessageBox.Show("请填写服务器地址");
                return;
            }
            string userName = txt_User.Text.Trim();
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
            string password = txt_Password.Text;
            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("密码不能为空");
                return;
            }
            string conStr = "server=" + serverIP + ";database=master;uid=" + userName + ";pwd=" + password + ";";
            string serverType = cb_DataBaseType.Text;
            MasterBLL bll = new MasterBLL(serverType);
            List<string> list = new List<string>();
            try
            {
                list = bll.GetDataBaseName(conStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            cb_DataBase.Items.Clear();
            foreach (string item in list)
            {
                if (!string.IsNullOrEmpty(item))
                    cb_DataBase.Items.Add(item);
            }
            if (cb_DataBase.Items.Count > 0)
            {
                cb_DataBase.SelectedIndex = 0;
                string dataBase = cb_DataBase.Text;
                string conT = "server=" + serverIP + ";database=" + dataBase + ";uid=" + userName + ";pwd=" + password + ";";
                Table_Load(conT);
            }
        }

        /// <summary>
        /// 加载表和视图
        /// </summary>
        /// <param name="conStr"></param>
        private void Table_Load(string conStr)
        {
            string serverType = cb_DataBaseType.Text;
            MasterBLL bll = new MasterBLL(serverType);
            List<string> listT = new List<string>();
            try
            {
                listT = bll.GetTableName(conStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            clb_TableName.Items.Clear();
            foreach (string item in listT)
            {
                clb_TableName.Items.Add(item);
            }

            List<string> listV = new List<string>();
            try
            {
                listV = bll.GetViewName(conStr);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            clb_ViewName.Items.Clear();
            foreach (string item in listV)
            {
                clb_ViewName.Items.Add(item);
            }
        }

        /// <summary>
        /// 生成文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Create_Click(object sender, EventArgs e)
        {
            string serverIP = txt_ServerIP.Text.Trim();
            if (string.IsNullOrEmpty(serverIP))
            {
                MessageBox.Show("请填写服务器地址");
                return;
            }
            string userName = txt_User.Text.Trim();
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
            string password = txt_Password.Text;
            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("密码不能为空");
                return;
            }
            string dataBase = cb_DataBase.Text;
            if (string.IsNullOrEmpty(dataBase))
            {
                MessageBox.Show("请选择数据库");
                return;
            }

            bool isDistinct = chbIsDistinct.Checked;
            bool isPascal = chbIsUpper.Checked;
            bool isCreateXML = chbIsCreateXML.Checked;
            bool isCreateSQL = chbIsCreateSQL.Checked;

            string conStr = "server=" + serverIP + ";database=" + dataBase + ";uid=" + userName + ";pwd=" + password + ";";

            if (folderBrowserPath.ShowDialog() == DialogResult.OK)
            {
                string path = folderBrowserPath.SelectedPath;
                string library = txt_Library.Text.Trim();
                string serverType = cb_DataBaseType.Text;
                MasterBLL bll = new MasterBLL(serverType, path + "\\", library, library);
                List<string> list = new List<string>();
                var lstT = clb_TableName.CheckedItems;
                foreach (var r in lstT)
                {
                    list.Add(r.ToString());
                }

                var lstV = clb_ViewName.CheckedItems;
                foreach (var r in lstV)
                {
                    list.Add(r.ToString());
                }

                if (list.Count > 0)
                {
                    bool bz = false;
                    try
                    {
                        bz = bll.CreateFile(conStr, list, isDistinct, isPascal, isCreateXML, isCreateSQL);
                    }
                    catch
                    {
                        MessageBox.Show("请选择正确的数据库类型");
                    }
                    if (bz)
                    {
                        MessageBox.Show("生成成功");
                    }
                }
                else
                {
                    MessageBox.Show("请选择视图或表");
                }
            }
        }

        /// <summary>
        /// 重新加载表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cb_DataBase_SelectedIndexChanged(object sender, EventArgs e)
        {
            string serverIP = txt_ServerIP.Text.Trim();
            if (string.IsNullOrEmpty(serverIP))
            {
                MessageBox.Show("请填写服务器地址");
                return;
            }
            string userName = txt_User.Text.Trim();
            if (string.IsNullOrEmpty(userName))
            {
                MessageBox.Show("用户名不能为空");
                return;
            }
            string password = txt_Password.Text;
            if (string.IsNullOrEmpty(password))
            {
                MessageBox.Show("密码不能为空");
                return;
            }
            string dataBase = cb_DataBase.Text;
            if (string.IsNullOrEmpty(dataBase))
            {
                MessageBox.Show("请选择数据库");
                return;
            }
            string conStr = "server=" + serverIP + ";database=" + dataBase + ";uid=" + userName + ";pwd=" + password + ";";
            Table_Load(conStr);
        }

        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateMappingFile_Load(object sender, EventArgs e)
        {
            this.Left = (int)((Screen.PrimaryScreen.Bounds.Width - this.Width) / 2);
            this.Top = (int)((Screen.PrimaryScreen.Bounds.Height - this.Height) / 2);
            this.Icon = Resource1.Setup;
        }
    }
}
